﻿using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class UITimedao : MonoBehaviour {

    public static UITimedao instance;

    private Text Daojishi;
     
    private float timer = 1f;

    private int starttime;
    // Use this for initialization
    void Awake () {
        instance = this;
        this.gameObject.SetActive(false);
        Daojishi = this.gameObject.GetComponent<Text>();
        starttime = 10;  //倒计时开始时间
    }


    // Time.timeScale 不能设置为0
    void Update()
    {
        //Debug.Log(timer + " -- " + starttime);
        timer -= Time.deltaTime;
        if(timer  <= 0 && starttime > 0)
        {
            //Debug.Log(timer + "  " + starttime);
            Daojishi.text = starttime.ToString();
            starttime--;
            timer = 1f;

            if (starttime <= 0)
            {
                SceneManager.LoadScene(0);
            }           
        }
    }

    //协程控制时间倒计时。。。 Time.timeScale 不能设置为0；
    //private void OnEnable()
    //{     
    //    timer = 10;
    //    InvokeRepeating("DaoJiShi", 0.1f, 1f);
    //    timer--;
    //}

    //private void OnDisable()
    //{
    //    StopAllCoroutines();
    //}

    //void DaoJiShi()
    //{
    //    Debug.Log(timer);
    //    Daojishi.text = timer.ToString();
    //}
}
